<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_search
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport( 'joomla.filesystem.file' );

$helper = JPATH_COMPONENT.'/helpers/glycophile.php';
require_once($helper);

/**
 *
 * @package		Joomla.Site
 * @subpackage	com_glycophile
 * @since 1.5
 */
class GlycophileModelSucres extends JModelLegacy
{
	/**
	 * data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	/**
	 * Helper
	 *
	 * @var object
	 */
	var $_helper = null;
	
	var $_serie = null;
	var $_subseries = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();

		//Get configuration
		$app	= JFactory::getApplication();
		$config = JFactory::getConfig();

		$this->_helper = new GlycophileHelper();

		// Get the pagination request variables
		$this->setState('limit', $app->getUserStateFromRequest('com_search.limit', 'limit', $config->get('list_limit'), 'uint'));
		$this->setState('limitstart', JRequest::getUInt('limitstart', 0));
	
		$this->countAllRows();
		$this->getList();
		$this->_serie = $this->_helper->getSerieById(JRequest::getVar('serie', 0));
		$this->_subseries = $this->_helper->getSubSeriesBySerieId(JRequest::getVar('serie', 0));
	}

	/**
	 * Method to get Sucre list
	 * Si theme sélectionné  (exemple Sujet divers id = 16 )
	 *
	 * @access public
	 * @return array
	 */
	 function getData()
	{
		return $this->_data;
	}
	
	function getSerie()
	{
		return $this->_serie;
	}
	
	function getSubSeries()
	{
		return $this->_subseries;
	}
	
	function getTotal()
	{
		return $this->_total;
	}
	
	function getPagination()
	{
		return $this->_pagination;
	}
	
	function getQuery($id_serie = 0, $id_pays = 0, $orderBy = null, $order = 'asc'){
		$query = "SELECT * FROM `#__glycophile_sucre` WHERE 1 ";
		$query .= $id_serie != 0 ? " AND `id_serie` = ".$id_serie : '';
		//$query .= $id_pays != 0 ? " AND `id_pays` = ".$id_pays : '';
		$query .= is_null( $orderBy ) ?  '' : " ORDER BY ". $orderBy ." ". $order;
    
		return $query ;
	}
	
	// TODO !!!!!!!!!!!!!!!!
	function getImgPathSucre($serieId , $inventaireId){
		
		$db = $this->getDbo();
		$query = "SELECT concat(s.id_pays, '/', t.code,'/', s.id, '/', i.id , '.jpg') URL
		FROM `#__glycophile_pays` p, #__glycophile_theme t, #__glycophile_serie s,
		#__glycophile_sousserie ss, #__glycophile_inventaire i 
		WHERE t.identpays= s.id_pays
		AND p.identpays = s.id_pays AND t.id= s.id_theme
		AND ss.id_serie = s.id 
		AND i.id_sous_serie = ss.id
		AND i.TailleX > 0 AND i.TailleY > 0
		AND s.id=".$serieId. " AND i.id=".$inventaireId;
		
		
		$db->setQuery($query);
		$url = $db->loadObjectList();
		return $url;
	}
	
	function countAllRows(){
		$db = $this->getDbo();
		$id_serie = JRequest::getVar('serie', 0);
		$id_pays = JRequest::getVar('pays', 0);
		$orderBy = JRequest::getVar('orderBy', 'rang');
		$order = JRequest::getVar('order', 'ASC');
		$query = $this->getQuery($id_serie, $id_pays, $orderBy, $order);
		$db->setQuery($query);
		$items = $db->query();
		$this->_total = $db->getNumRows();
	}
	 
	function getList()
	{
		$db = $this->getDbo();
		global $mainframe;
		$config = JFactory::getConfig();
		
		$limitstart = JRequest::getUInt('limitstart', 1);
		$limit = $config->getValue('config.list_limit');
		$ligne = ($limitstart - 1) * $limit;
		
		$id_serie = JRequest::getVar('serie', 0);
		$id_pays = JRequest::getVar('pays', 0);
		$orderBy = JRequest::getVar('orderBy', 'rang');
		$order = JRequest::getVar('order', 'ASC');
		$query = $this->getQuery($id_serie, $id_pays, $orderBy, $order);
		$query .= " Limit ". $ligne .", ". $limit; 

		$db->setQuery($query);
		$sucres = $db->loadObjectList();
		
		$imgs = array();
		
		foreach($sucres as $sucre){

			$q = "SELECT *
			FROM `#__glycophile_inventaire`
			WHERE `id_sucre` =". $sucre->id ;
			$db->setQuery($q);
			$inventaires = $db->loadObjectList();
      
			foreach($inventaires as $inventaire){
				$imgs = $this->getImgPathSucre($sucre->id_serie , $inventaire->id);
			}

			if(isset($imgs[0]->URL)){
				
				$ima = 'components/com_glycophile/asset/images/'.$imgs[0]->URL ;
				$imageData = $this->_helper->preAffiche_Image3($ima, 0.33, $sucre->designation);
				
				$sucre->imgPath = $imageData['imgPath'] ;
				$sucre->rimgPath = $imageData['rimgPath'] ;
				$sucre->img = $imageData['img'];
				if(isset($imageData['rimg'])) { $sucre->rimg = $imageData['rimg']; }
			}
		}
		
		$this->_data = $sucres;
		
		$Itemid = JRequest::getVar('Itemid', 0);
		$url = 'index.php?option=com_glycophile&view=sucres&pays='.$id_pays.'&serie='. $id_serie .'&Itemid='.$Itemid;
		$url .= '&orderBy='. $orderBy .'&order='.$order;
		$this->_pagination = $this->_helper->getPaginationBar($this->_total, $limit, $url);
	}


}
